﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class ChangePwdAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request.Form["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {

                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        public void ChangePwd()
        {
            string oldPwd = Request.Form["pwd"].ToString();
            string newPwd = Request.Form["pwd1"].ToString();
            string account = Request.Form["account"].ToString();
            int count = DALSession.Current.BD_UserInfoDAL.GetCount(u => u.ID == OperateContext.SessBasicInfo.UserInfo.Id && u.PassWord == oldPwd);
            if (count == 0)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "原始密码错误"));

            }
            else if (DALSession.Current.BD_UserInfoDAL.GetCount(u => u.AccountLogIn == account) != 0 && account != OperateContext.SessBasicInfo.UserInfo.AccountLogIn)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "该帐号已存在，无法保存"));
            }
            else
            {

                DALSession.Current.BD_UserInfoDAL.ModifyBy(new BD_UserInfo() { ID = OperateContext.SessBasicInfo.UserInfo.Id, PassWord = newPwd, AccountLogIn = account }, u => u.ID == OperateContext.SessBasicInfo.UserInfo.Id, "PassWord", "AccountLogIn");
                DALSession.Current.BD_UserInfoDAL.SaveChanges();
                Response.Write(JsonHelper.toDefaultMsg(true));

            }
        }
    }
}